package com.keqiang.chart.impl

import android.graphics.Canvas
import android.graphics.Color
import android.text.Layout
import android.text.TextPaint
import androidx.annotation.ColorInt
import androidx.annotation.Px
import com.keqiang.chart.base.IEmptyDataRender
import com.keqiang.chart.utils.ObjectPool
import com.keqiang.chart.utils.px
import com.keqiang.utils.PaintUtils

/**
 * 空数据时的绘制器
 *
 * @param defText 提示的文本
 * @param maxLines 文本绘制的行数
 * @param ellipsize 文本超出最大行数是否显示省略号
 *
 * @author Created by wanggaowan on 2022/8/10 14:54
 */
class EmptyDataRender @JvmOverloads constructor(
    val defText: String,
    @Px
    val textSize: Float = 24f.px,
    @ColorInt
    val textColor: Int = Color.GRAY,
    val maxLines: Int = Int.MAX_VALUE,
    val ellipsize: Boolean = true
) : IEmptyDataRender {

    override fun onDraw(canvas: Canvas, start: Float, top: Float, end: Float, bottom: Float): Boolean {
        val textPaint = ObjectPool.obtainPaint<TextPaint>(true)
        textPaint.textSize = textSize
        textPaint.color = textColor

        val width = end - start
        val height = bottom - start

        val textHeight = PaintUtils.getTextHeight(textPaint, defText, width.toInt(), maxLines)
        canvas.save()
        canvas.translate(0f, (height / 2f - textHeight / 2f))
        PaintUtils.drawText(canvas, textPaint, defText, width.toInt(), maxLines, Layout.Alignment.ALIGN_CENTER, ellipsize)
        canvas.restore()

        ObjectPool.recyclePaint(textPaint)
        return true
    }
}